package com.litong.litongjiaoyu.controller;

import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.entity.SysCampus;
import com.litong.litongjiaoyu.service.CampusService;
import com.litong.litongjiaoyu.utils.JSONResult;
import com.litong.litongjiaoyu.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName CampusController
 * @Description 校区管理控制层
 * @Author jingz
 * @Date 2020/1/17 17:02
 * @Version 1.0
 **/
@RestController
@RequestMapping("/campus")
public class CampusController {
    
    private Logger logger = LoggerFactory.getLogger(CampusController.class);
    @Autowired
    private CampusService campusService;

    @Autowired
    private RedisUtil redisUtil;

    //跳过权限验证
    @IgnoreAuth
    @GetMapping("/query")
    public JSONResult query() {
        logger.info("开始获取全部校区信息");
        logger.info("获取完成");
        return JSONResult.ok(redisUtil.lGet("Campus", 0, -1));
    }

    @IgnoreAuth
    @PostMapping("/getCampus")
    public JSONResult getCampus(long id) {
        logger.info("开始获取id:" + id + "校区数据");
        logger.info("获取完成");
        return JSONResult.ok(campusService.getCampus(id));
    }

    @IgnoreAuth
    @GetMapping("/getName")
    public JSONResult getName() {
        logger.info("开始获取校区名称");
        try {
            logger.info("校区名称获取成功");
            return JSONResult.ok(campusService.getCampusName());
        } catch (Exception e) {
            logger.error("校区名称获取失败：\n" + e.getMessage());
            return JSONResult.errorMsg(e.getMessage());
        }
    }


    @PostMapping("/insert")
    public JSONResult insert(@RequestBody SysCampus campus) {
        try {
            if(campus != null) {
                logger.info("开始添加校区信息");

                if(campusService.insetCampus(campus) > 0) {
                    logger.info("存入数据库成功");
                    return JSONResult.ok();
                } else {
                    logger.info("存入数据库失败");
                    return JSONResult.errorMsg("Deposit failed");
                }
            } else {
                logger.info("campus 为空");
                return JSONResult.errorMsg("Data:campus is empty");
            }
        } catch (Exception e) {
            logger.error("方法:CampusController -> insert发生错误\n" + e.getMessage());
            return JSONResult.errorException("An error occurred, and the work was suspended");
        }
    }

    @PostMapping("/update")
    public JSONResult update(@RequestBody SysCampus campus) {
        if(campus != null) {
            logger.info("修改id:" + campus.getId() + "数据");

            if(campusService.update(campus) > 0) {
                logger.info("数据修改成功");
                return JSONResult.ok();
            } else {
                logger.info("数据修改失败");
                return JSONResult.errorMsg("fail to edit");
            }
        } else {
            logger.info("campus 为空");
            return JSONResult.errorMsg("Data:campus is empty");
        }
    }


    @PostMapping("delete")
    public JSONResult delete(long id) {
        logger.info("删除id:" +id + "的数据");

        int s = campusService.delete(id);

        if(s > 0) {
            logger.info(id + "删除成功");
            return JSONResult.ok();
        } else {
            logger.error( id + "数据修改失败");
            return JSONResult.errorMsg("fail to delete");
        }
    }
}
